In [16]:
def count_words(sentence):
    """
    Function that takes a string as argument and returns
    a dictionary which has words as the keys and the number
    of times each word was seen in the sentence as the value.
    """
    
    return {word:sentence.count(word) for word in sentence.lower().split()}

In [21]:
count_words("oh what a day what a lovely day")   # wrong: 'a' is counted for the whole sentence


Out[21]:
{'a': 6, 'day': 2, 'lovely': 1, 'oh': 1, 'what': 2}

In [22]:
count_words("don't stop believing")


Out[22]:
{'believing': 1, "don't": 1, 'stop': 1}

In [19]:
count_words("Oh what a day what a lovely day")  # wrong: 'oh' does not seem to be counted


Out[19]:
{'a': 6, 'day': 2, 'lovely': 1, 'oh': 0, 'what': 2}

In [23]:
count_words("Oh what a day, what a lovely day!")  # wrong: day! and day, are counted as different


Out[23]:
{'a': 6, 'day!': 1, 'day,': 1, 'lovely': 1, 'oh': 0, 'what': 2}

Refining #1: Considering mixed case words


In [24]:
def count_words(sentence):
    """
    Function that takes a string as argument and returns
    a dictionary which has words as the keys and the number
    of times each word was seen in the sentence as the value.
    """
    
    words = sentence.lower().split()
    return {word:words.count(word) for word in words}

In [25]:
count_words("oh what a day what a lovely day")


Out[25]:
{'a': 2, 'day': 2, 'lovely': 1, 'oh': 1, 'what': 2}

In [26]:
count_words("don't stop believing")


Out[26]:
{'believing': 1, "don't": 1, 'stop': 1}

In [27]:
count_words("Oh what a day what a lovely day")


Out[27]:
{'a': 2, 'day': 2, 'lovely': 1, 'oh': 1, 'what': 2}

In [28]:
count_words("Oh what a day, what a lovely day!")  # wrong: day! and day, are counted as different


Out[28]:
{'a': 2, 'day!': 1, 'day,': 1, 'lovely': 1, 'oh': 1, 'what': 2}

Experiments


In [30]:
import string

In [31]:
string.punctuation


Out[31]:
'!"#$%&\'()*+,-./:;<=>?@[\\]^_`{|}~'

Refining #2: Ignoring punctuations outside of words


In [40]:
def count_words(sentence):
    """
    Function that takes a string as argument and returns
    a dictionary which has words as the keys and the number
    of times each word was seen in the sentence as the value.
    """
    
    words = sentence.lower().split()
    words = [word.strip(string.punctuation) for word in words]   # Removing punctuations outside of words
    return {word:words.count(word) for word in words}

In [41]:
count_words("oh what a day what a lovely day")


Out[41]:
{'a': 2, 'day': 2, 'lovely': 1, 'oh': 1, 'what': 2}

In [42]:
count_words("don't stop believing")


Out[42]:
{'believing': 1, "don't": 1, 'stop': 1}

In [43]:
count_words("Oh what a day what a lovely day")


Out[43]:
{'a': 2, 'day': 2, 'lovely': 1, 'oh': 1, 'what': 2}

In [44]:
count_words("Oh what a day, what a lovely day!")


Out[44]:
{'a': 2, 'day': 2, 'lovely': 1, 'oh': 1, 'what': 2}

In [45]:
import unittest


class CountWordsTests(unittest.TestCase):

    """Tests for count_words."""

    def test_simple_sentence(self):
        actual = count_words("oh what a day what a lovely day")
        expected = {'oh': 1, 'what': 2, 'a': 2, 'day': 2, 'lovely': 1}
        self.assertEqual(actual, expected)

    def test_apostrophe(self):
        actual = count_words("don't stop believing")
        expected = {"don't": 1, 'stop': 1, 'believing': 1}
        self.assertEqual(actual, expected)

    # To test the Bonus part of this exercise, comment out the following line
    #@unittest.expectedFailure
    def test_capitalization(self):
        actual = count_words("Oh what a day what a lovely day")
        expected = {'oh': 1, 'what': 2, 'a': 2, 'day': 2, 'lovely': 1}
        self.assertEqual(actual, expected)

    # To test the Bonus part of this exercise, comment out the following line
    #@unittest.expectedFailure
    def test_symbols(self):
        actual = count_words("Oh what a day, what a lovely day!")
        expected = {'oh': 1, 'what': 2, 'a': 2, 'day': 2, 'lovely': 1}
        self.assertEqual(actual, expected)


if __name__ == "__main__":
    unittest.main(argv=['first-arg-is-ignored'], exit=False)


....
----------------------------------------------------------------------
Ran 4 tests in 0.003s

OK

In [ ]: